package com.ruoyi.jwxt.utility;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jwxt.domain.PO.JwxtLabMaintenance;
import com.ruoyi.jwxt.domain.PO.JwxtStudentApplication;
import com.ruoyi.jwxt.domain.PO.JwxtTeacherApplication;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Component
public class JwxtUtils {

    @Autowired
    private SysConfigServiceImpl sysConfigService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 获取当前全局学期
     * @return 字符串类型的学期名称"20XX-20XX-1/2"
     */
    public String getCurSemester() {
        return sysConfigService.selectConfigByKey("jwxt.curSemester");
    }

    /**
     * 当前用户是（超级）管理员吗？
     * @return 布尔类型
     */
    public boolean isAdmin() {
        if (SecurityUtils.hasRole("admin") || SecurityUtils.hasRole("root")) {
            return true;
        }
        return false;
    }

    /**
     * 获取某角色的所有用户的<userId,userName>表单
     */
    public List<SysUser> getRoleList(String target) {

        List<SysUser> userList = new ArrayList<>();
        Map<Long, String> map = new LinkedHashMap<>();
        // 获取目标角色ID
        Long targetId = null;
        List<SysRole> allRole = sysRoleService.selectRoleAll();
        for (SysRole role : allRole) {
            if (role.getRoleKey().equals(target) || role.getRoleName().equals(target)) {
                targetId = role.getRoleId();
                break;
            }
        }
        // 获取目标用户ID列表
        List<Long> userIdList = sysUserRoleMapper.getUserIdByRoleId(targetId);
        Collections.sort(userIdList);
        // 根据目标用户ID列表获得用户名称
        for (Long userId : userIdList) {
            SysUser user = sysUserService.selectUserById(userId);
            userList.add(user);
        }
        return userList;
    }
    public static List<JwxtTeacherApplication> teaFilterByUser(List<JwxtTeacherApplication> list){
        if(SecurityUtils.hasRole("root")||SecurityUtils.hasRole("admin"))
            return list;
        List<JwxtTeacherApplication> tmp=new ArrayList<>();
        for (JwxtTeacherApplication m :
                list) {
            if (m.getApplicantId()==SecurityUtils.getUserId()){
                tmp.add(m);
            }
        }
        return tmp;
    }
    public static List<JwxtStudentApplication> stuFilterByUser(List<JwxtStudentApplication> list){
        if(SecurityUtils.hasRole("root")||SecurityUtils.hasRole("admin"))
            return list;
        List<JwxtStudentApplication> tmp=new ArrayList<>();
        for (JwxtStudentApplication m :
                list) {
            if (m.getApplicantId()==SecurityUtils.getUserId()){
                tmp.add(m);
            }
        }
        return tmp;
    }
    public static List<JwxtLabMaintenance> maintenancesFilterByUser(List<JwxtLabMaintenance> list){
        if(SecurityUtils.hasRole("root")||SecurityUtils.hasRole("admin"))
            return list;
        List<JwxtLabMaintenance> tmp=new ArrayList<>();
        for (JwxtLabMaintenance m :
                list) {
            if (m.getExperimenterId() == SecurityUtils.getUserId()
                    ||m.getTeacherId()==SecurityUtils.getUserId()){
                tmp.add(m);
            }
        }
        return tmp;
    }
}
